//
//  QuickGuidePageViewController.swift
//  CoolTennisBall
//
//  Created by CA on 16/5/16.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

import UIKit
import EAIntroView

class QuickGuidePageViewController: UIViewController {

    enum GuideType {
        case quickGuide
        case sportSkill
        case foreBackHand
        case curveLine
        case speed
        case hdQuickGuide
        
        var pageCount: Int {
            switch self {
            case .quickGuide: return 10
            case .sportSkill: return 3
            case .foreBackHand: return 1
            case .curveLine: return 1
            case .speed: return 1
            case .hdQuickGuide: return 6
            }
        }
        
        var guideStoryboardId: String {
            switch self {
            case .quickGuide: return "QuickGuidePage"
            case .sportSkill: return "SportSkillGuidePage"
            case .foreBackHand: return "HandTypeGuidePage"
            case .curveLine: return "CurveLineGuidePage"
            case .speed: return "SportSpeedGuidePage"
            case .hdQuickGuide: return "HDQuickGuidePage"
            }
        }
       
        var pageControlMarginBottom: CGFloat {
            switch self {
            case .quickGuide, .hdQuickGuide: return 420
            case .sportSkill, .foreBackHand, .curveLine, .speed:
                if Bundle.currentLanguageIsChinese() {
                    return 220
                }else {
                    return 300;
                }
                
            }
        }
    }
    
    var currentIndexPage = 0
    var objc_guideTypeNum = 0
    
    var guideType: GuideType {
        switch objc_guideTypeNum {
        case 0: return GuideType.quickGuide
        case 1: return GuideType.sportSkill
        case 2: return GuideType.foreBackHand
        case 3: return GuideType.curveLine
        case 4: return GuideType.speed
        case 5: return GuideType.hdQuickGuide
        default: fatalError("毛这个类型的说明喔！")
        }
    }
    
    // MARK: View LifeCycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(white: 0, alpha: 0.4)
        showQuickGuideWithCount(guideType.pageCount, currentPage: currentIndexPage)
    }
    
    // MARK: IBAction
    
    func closeQuickGuide(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
    // MARK: Private Method
    
    fileprivate func showQuickGuideWithCount(_ count: Int, currentPage: Int) {
        var stroryBoardName = String()
        
         stroryBoardName = "QuickGuide"
        
        if User.getLast().userDevice.oemType == OemType.HD && objc_guideTypeNum == 5 {
            stroryBoardName = "HDQuickGuide"
        }
        
        let storyboard = UIStoryboard(name: stroryBoardName, bundle: nil)
        
        let guidePages: [EAIntroPage] = (0..<count).map {
            let page = EAIntroPage()
            
            let controller = storyboard.instantiateViewController(withIdentifier: "\(guideType.guideStoryboardId)\($0)")
            page.customView = controller.view
            if let closeButton = controller.view.viewWithTag(100) as? UIButton {
                closeButton.addTarget(self, action: #selector(QuickGuidePageViewController.closeQuickGuide(_:)), for: .touchUpInside)
            }
            
            return page
        }
        
        let intro =  EAIntroView(frame: view.bounds, andPages: guidePages)
        intro?.skipButton.isHidden = true
        intro?.swipeToExit = false
        intro?.pageControl.isHidden = guideType.pageCount <= 1
        intro?.pageControl.currentPageIndicatorTintColor = UIColor(red: 37/255.0, green: 201/255.0, blue: 173/255.0, alpha: 1.0)
        intro?.pageControl.pageIndicatorTintColor = UIColor.darkGray
        
        if guideType.pageControlMarginBottom == 300 {
             intro?.pageControlY =  (view.bounds.height - guideType.pageControlMarginBottom + 30)/2
        }else {
             intro?.pageControlY =  (view.bounds.height - guideType.pageControlMarginBottom)/2
        }

        intro?.show(in: view, animateDuration: 0)
        intro?.currentPageIndex = UInt(currentPage)
    
    }
    
}


